package com.project.bookstore.common;

import com.project.bookstore.exception.ServiceException;
import com.project.bookstore.order.service.OrderService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("通用返回结果")
public class CommonResult<T>  {
    @ApiModelProperty("响应状态码")
    private Integer state;
    @ApiModelProperty("响应消息")
    private String message;
    @ApiModelProperty("响应数据")
    private T data;

    public static CommonResult<Void> fail(State state, String message) {
        CommonResult<Void> result = new CommonResult<>();
        result.setState(state.getState());
        result.setMessage(message);
        return result;
    }
    public static CommonResult<Void> fail(State state) {
        return fail(state,null);
    }
    public static CommonResult<Void> fail(ServiceException e) {
        return new CommonResult<>(e.getState(), e.getMessage(), null);
    }

    public static CommonResult<Void> ok() {
        return ok(null);
    }

    public static <T> CommonResult<T> ok(T data) {
        CommonResult<T> commonResult = new CommonResult<>(State.OK);
        commonResult.setData(data);
        return commonResult;
    }

    private CommonResult() {
    }

    private CommonResult(Integer state, String message, T data) {
        this.state = state;
        this.message = message;
        this.data = data;
    }

    public CommonResult(State state) {
        this.state = state.getState();
    }
}
